// weixinmao_cars/business/login/login.js
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        login: {
            username: '',
            password: '',
            handle_type: 'login',
            openid : ''
        },
      showpass:true,
      focusPass: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        if(!options.expire){
            if (wx.getStorageSync('supplierToken')) {
                this.navtoindex();
            }
        }
    },

  clickshow(e){
    if (this.data.showpass){
      this.setData({
        showpass: false,
      })
    }else{
      this.setData({
        showpass: true,
      })
    }
  },

  toPass(e){
    this.setData({
      focusPass:true
    })
  },
    username: function(e) {
        var val = e.detail.value;
        this.setData({
            ['login.username']: val
        });
    },
    password: function(e) {
        var val = e.detail.value;
        this.setData({
            ['login.password']: val
        });
    },
    /**
     * 商家登录 ,账号密码登录
     */
    userLogin: function() {
        var that = this;
        let openid = wx.getStorageSync('openid');
        if (!openid) {
            wx.login({
                success: function (res) {
                    let code = res.code
                    app.util.request({
                        url: "entry/wxapp/openid",
                        method: 'POST',
                        data: {
                            code: code,
                        },
                        success: function (res) {
                            let t = res.data.data;
                            wx.setStorageSync("openid", t.data.openid);
                            wx.setStorageSync("token", t.token.token);
                            wx.setStorageSync("supplier_id", t.data.supplier_id);

                            that.setData({
                                ['login.handle_type']: 'login',
                                ['login.openid']: t.data.openid
                            });
                            that.login();
                        },
                    })
                }
            })
        }else{
            that.setData({
                ['login.handle_type']: 'login',
                ['login.openid']: openid
            });
            that.login();
        }
       
    },
    /**
     * 商家登录,使用微信快捷登录
     */
    wechatLogin: function() {
        var that = this;
        this.setData({
            ['login.handle_type']: 'bind'
        });
        let openid = wx.getStorageSync('openid');
        if (openid){
            that.autoLogin(openid);
        }else{
            wx.login({
                success: function(res) {
                    let code = res.code
                    app.util.request({
                        url: "entry/wxapp/openid",
                        method: 'POST',
                        data: {
                            code: code,
                        },
                        success: function(res) {
                            let t = res.data.data;
                            wx.setStorageSync("openid", t.data.openid);
                            wx.setStorageSync("token", t.token.token);
                          wx.setStorageSync("supplier_id", t.data.supplier_id);
                            that.autoLogin(t.data.openid);
                        },
                    })
                }
            })
        }
    },
    login: function() {
        var that = this;
        if (!that.data.login.username) {
            wx.showModal({
                title: '提示',
                content: '请输入账号',
                showCancel: false
            })
            return false;
        }
        if (!that.data.login.password) {
            wx.showModal({
                title: '提示',
                content: '请输入密码',
                showCancel: false
            })
            return false;
        }
        app.util.request({
            url: "entry/wxapp/SupplierLogin",
            cachetime: "0",
            showLoading: 1,
            data: that.data.login,
            success: function(t) {
                //保存商家登录的token
                var rs = t.data;
                if (rs.data.token) {
                    wx.setStorageSync("supplierToken", rs.data.token);
                  wx.setStorageSync("supplier_id", rs.data.supplier_id);
                }
                that.navtoindex();
            }
        });
    },
    autoLogin:function(openid){
        var that = this;
        app.util.request({
            url: "entry/wxapp/WechatLogin",
            method: 'GET',
            data: {
                openid: openid,
            },
            success: function (res) {
                let t = res.data.data;
                wx.setStorageSync("supplierToken", t.token);
              wx.setStorageSync("supplier_id", t.supplier_id);
                that.navtoindex();
            },
        })
    },
    navtoindex: function() {
        wx.redirectTo({
          url: '../index/index',
        })
    }
})